package com.lebang.service.sys;

import com.lebang.entity.sys.SysResource;
import com.lebang.mapper.sys.SysResourceMapper;
import com.lebang.model.enums.Booleans;
import com.lebangweb.common.UUIDUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author hwq
 */
@Service
public class SysResourceService {
    @Autowired
    private SysResourceMapper sysResourceMapper;


    public JsonData insert(SysResource po) {
        po.setResource(UUIDUtil.M());
        po.setParams("[]");
        po.setHasDeleted(Booleans.False.name());
        if ("0".equals(po.getParent())) {
            po.setCategory("Menu");
        } else {
            po.setCategory("MenuItem");
        }
        po.setModifyTime(new Date());
        po.setCreateTime(new Date());
        int init = sysResourceMapper.insertSelective(po);
        if (init > 0) {
            return Callback.success("200", "新增成功", null);
        } else {

            return Callback.fail("403", "新增失败", null);
        }
    }


    public JsonData update(SysResource po) {
        po.setParams("[]");
        po.setHasDeleted(Booleans.False.name());
        if ("0".equals(po.getParent())) {
            po.setCategory("Menu");
        } else {
            po.setCategory("MenuItem");
        }
        po.setModifyTime(new Date());
        int init = sysResourceMapper.updateByPrimaryKeySelective(po);
        if (init > 0) {
            return Callback.success("200", "修改成功", null);
        } else {

            return Callback.fail("403", "修改失败", null);
        }
    }
    public SysResource selectByPrimaryKey(Long id) {
        return sysResourceMapper.selectByPrimaryKey(id);
    }

    public List<Map<String, Object>> findAllparent() {
        return sysResourceMapper.findAllparent();
    }
}
